package ptotocol

import (
	"fmt"
	"io"
)

type Writer struct {
	writer io.Writer
}

func NewWriter(writer io.Writer) *Writer {
	return &Writer{
		writer: writer,
	}
}

func (w *Writer) WriteString(msg string) error {
	_, err := w.writer.Write([]byte(msg))
	return err
}

func (w *Writer) Write(command interface{}) error {
	var err error
	switch cmd := command.(type) {
	case SendCmd:
		err = w.WriteString(fmt.Sprintf("SEND %v \n", cmd.Msg))
	case NameCmd:
		err = w.WriteString(fmt.Sprintf("NAME MSG %s\n", cmd.Name))
	case MessCmd:
		err = w.WriteString(fmt.Sprintf("MESS MSG %v %v\n", cmd.Name, cmd.Msg))
	default:
		err = UnknownCommand
	}

	return err

}
